//
//  NewsViewController.m
//  OtakuWorld
//
//  Created by iOS Two on 14-9-19.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsInfoViewController.h"
#import "UIImageView+MJWebCache.h"

@interface NewsViewController ()

@end

@implementation NewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillDisappear:(BOOL)animated {
    [MobClick endLogPageView:@"NewsViewController"];
}
-(void)viewWillAppear:(BOOL)animated {
    [super.navigationController setNavigationBarHidden:YES animated:TRUE];
    [super.navigationController setToolbarHidden:YES animated:TRUE];
    [MobClick beginLogPageView:@"NewsViewController"];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImageView *nav = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, 64)];
    nav.image = [UIImage imageNamed:@"qwe_02"];
    [self.view addSubview:nav];
    
    _monthArray = [NSArray arrayWithObjects:@"全部",@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月" ,nil];
    _province = @"";
    [self creatController];
    _url = [NSURL URLWithString:@"http://nyato.com/index.php?app=exhibition&mod=Adapi&act=ex_list&keyword=&province=&month="];
    [self getFirstData];
    // Do any additional setup after loading the view.
}
-(void)creatController {
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 61, 320, 3)];
    line.backgroundColor = [UIColor colorWithRed:0.92 green:0.41 blue:0.28 alpha:1];
    [self.view addSubview:line];
    
    _place = [UIButton buttonWithType:UIButtonTypeCustom];
    _place.frame = CGRectMake(184, 38, 66, 26);
    [_place setTitle:@"地点" forState:UIControlStateNormal];
    _place.titleLabel.font = [UIFont fontWithName:@"STXIHEI" size:16];
    _place.backgroundColor = [UIColor colorWithRed:0.92 green:0.41 blue:0.28 alpha:1];
    [_place addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    _place.tag = 1;
    _place.layer.masksToBounds = YES;
    _place.layer.cornerRadius = 3.0;
//    _place.layer.borderWidth = 1.0;
//    _place.layer.borderColor = [[UIColor whiteColor] CGColor];
    [self.view addSubview:_place];
    
    _month = [UIButton buttonWithType:UIButtonTypeCustom];
    _month.frame = CGRectMake(254, 38, 60, 26);
    [_month setTitle:@"月份" forState:UIControlStateNormal];
    _month.titleLabel.font = [UIFont fontWithName:@"STXIHEI" size:16];

    [_month addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    _month.backgroundColor = [UIColor colorWithRed:0.92 green:0.41 blue:0.28 alpha:1];
    _month.tag = 2;
    _month.layer.masksToBounds = YES;
    _month.layer.cornerRadius = 3.0;
    [self.view addSubview:_month];
    
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, kHeight - 49 - 64) style:UITableViewStylePlain];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    _mainTable.tag = 3;
    _mainTable.rowHeight = 79;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTable];
    
    _placeTable = [[UITableView alloc]initWithFrame:CGRectMake(184, 64, 66, 0) style:UITableViewStylePlain];
    _placeTable.delegate = self;
    _placeTable.dataSource = self;
    _placeTable.tag = 1;
    _placeTable.rowHeight = 29;
    _placeTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _placeTable.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_placeTable];
    
    _monthTable = [[UITableView alloc]initWithFrame:CGRectMake(254, 64, 60, 0) style:UITableViewStylePlain];
    _monthTable.delegate = self;
    _monthTable.dataSource = self;
    _monthTable.tag = 2;
    _monthTable.rowHeight = 29;
    _monthTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _monthTable.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_monthTable];
    
//    _scrollProxy = [[NJKScrollFullScreen alloc] initWithForwardTarget:self]; // UIScrollViewDelegate and UITableViewDelegate methods proxy to ViewController
//    
//    _mainTable.delegate = (id)_scrollProxy; // cast for surpress incompatible warnings
//    
//    _scrollProxy.delegate = self;
}
-(void)buttonClick:(UIButton *)sender {
    if (sender.tag == 1) {
        if (_placeTable.frame.size.height != 0) {
            [UIView animateWithDuration:0.5 animations:^{
                _placeTable.frame = CGRectMake(184, 64, 66, 0);
            }completion:^(BOOL finished) {
                NSLog(@"动画执行完成后  要做的事情");
            }];
        } else {
            if (_monthTable.frame.size.height != 0) {
                [UIView animateWithDuration:0.5 animations:^{
                    _monthTable.frame = CGRectMake(254, 64, 60, 0);
                }completion:^(BOOL finished) {
                    NSLog(@"动画执行完成后  要做的事情");
                }];
            }
            [UIView animateWithDuration:0.5 animations:^{
                _placeTable.frame = CGRectMake(184, 64, 66, 300);
            }completion:^(BOOL finished) {
                NSLog(@"动画执行完成后  要做的事情");
            }];
        }
    } else if (sender.tag == 2) {
        if (_monthTable.frame.size.height != 0) {
            [UIView animateWithDuration:0.5 animations:^{
                _monthTable.frame = CGRectMake(254, 64, 60, 0);
            }completion:^(BOOL finished) {
                NSLog(@"动画执行完成后  要做的事情");
            }];
        } else {
            if (_placeTable.frame.size.height != 0) {
                [UIView animateWithDuration:0.5 animations:^{
                    _placeTable.frame = CGRectMake(184, 64, 66, 0);
                }completion:^(BOOL finished) {
                    NSLog(@"动画执行完成后  要做的事情");
                }];
            }
            [UIView animateWithDuration:0.5 animations:^{
                _monthTable.frame = CGRectMake(254, 64, 60, 300);
            }completion:^(BOOL finished) {
                NSLog(@"动画执行完成后  要做的事情");
            }];
        }
    }
}
-(void)getFirstData {
    [self showIndeterminateProgress];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:_url];
    request.delegate = self;
    request.username = @"place";
    [request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request {
    SBJsonParser *jsonP = [[SBJsonParser alloc]init];
    if ([request.username isEqualToString:@"place"]) {
        if (_provinceArray == nil) {
            _provinceArray = [[NSMutableArray alloc]init];
            _placeArray = [[NSMutableArray alloc]init];
            [_provinceArray addObject:@""];
            [_placeArray addObject:@"全部"];
            NSArray *array =[[jsonP objectWithData:[request responseData]]objectForKey:@"province"];
            for (int i = 0; i < array.count; i++) {
                [_provinceArray addObject:[[array objectAtIndex:i] objectForKey:@"province"]];
                [_placeArray addObject:[[array objectAtIndex:i] objectForKey:@"title"]];
            }
            [_placeTable reloadData];
        }
        NSArray *array1 = [[jsonP objectWithData:[request responseData]] objectForKey:@"data"];
        if (array1.count == 0) {
            [self alertViewShow:@"暂无漫展信息"];
            return;
        }
        if (_imgArray == nil) {
            _imgArray = [[NSMutableArray alloc]init];
            _startArray = [[NSMutableArray alloc]init];
            _eidArray = [[NSMutableArray alloc]init];
            _endArray = [[NSMutableArray alloc]init];
            _nameArray = [[NSMutableArray alloc]init];
            _sectionArray = [[NSMutableArray alloc]init];
            _locationArray = [[NSMutableArray alloc]init];
        }
        [_imgArray removeAllObjects];
        [_startArray removeAllObjects];
        [_eidArray removeAllObjects];
        [_endArray removeAllObjects];
        [_nameArray removeAllObjects];
        [_sectionArray removeAllObjects];
        [_locationArray removeAllObjects];
        for (int i = 0; i < array1.count; i++) {
            [_imgArray addObject:[[array1 objectAtIndex:i] objectForKey:@"cover"]];
            [_startArray addObject:[[array1 objectAtIndex:i] objectForKey:@"start_time"]];
            [_endArray addObject:[[array1 objectAtIndex:i] objectForKey:@"end_time"]];
            [_nameArray addObject:[[array1 objectAtIndex:i] objectForKey:@"name"]];
            [_locationArray addObject:[[array1 objectAtIndex:i] objectForKey:@"location"]];
            [_eidArray addObject:[[array1 objectAtIndex:i] objectForKey:@"eid"]];
            [_sectionArray addObject:[[array1 objectAtIndex:i] objectForKey:@"section_name"]];
        }
        [_mainTable reloadData];
    }
    [HUD hide:YES];
}
-(void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"请求失败");
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 1) {
        return _placeArray.count;
    } else if (tableView.tag == 2) {
        return 13;
    } else if (tableView.tag == 3) {
        return _sectionArray.count;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        } else {
            for (UIView *view in [cell.contentView subviews]) {
                [view removeFromSuperview];
            }
        }
        cell.backgroundColor = [UIColor colorWithRed:0.92 green:0.41 blue:0.28 alpha:1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 66, 29)];
        label.text = [_placeArray objectAtIndex:indexPath.row];
        label.font = [UIFont fontWithName:@"STXIHEI" size:15];
        label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label];
        
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 28, 66, 1)];
        line.backgroundColor = [UIColor colorWithRed:0.75 green:0.31 blue:0.2 alpha:1];
        [cell.contentView addSubview:line];
        
        if ([[_placeArray objectAtIndex:indexPath.row] isEqualToString:@"全部"]) {
            label.textColor = [UIColor colorWithRed:0.99 green:0.9 blue:0.2 alpha:1];
            line.backgroundColor = [UIColor colorWithRed:0.99 green:0.9 blue:0.2 alpha:1];
        }
        return cell;
    } else if (tableView.tag == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        } else {
            for (UIView *view in [cell.contentView subviews]) {
                [view removeFromSuperview];
            }
        }
        cell.backgroundColor = [UIColor colorWithRed:0.92 green:0.41 blue:0.28 alpha:1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 29)];
        label.text = [_monthArray objectAtIndex:indexPath.row];
        label.font = [UIFont fontWithName:@"STXIHEI" size:15];
        label.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label];
        
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 28, 60, 1)];
        line.backgroundColor = [UIColor colorWithRed:0.75 green:0.31 blue:0.2 alpha:1];
        [cell.contentView addSubview:line];
        
        if ([[_monthArray objectAtIndex:indexPath.row] isEqualToString:@"全部"]) {
            label.textColor = [UIColor colorWithRed:0.99 green:0.9 blue:0.2 alpha:1];
            line.backgroundColor = [UIColor colorWithRed:0.99 green:0.9 blue:0.2 alpha:1];
        }
        return cell;
    } else if (tableView.tag == 3) {
        _cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if(_cell == nil){
            _cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        } else {
            for (UIView *view in [_cell.contentView subviews]) {
                [view removeFromSuperview];
            }
        }
        _cell.selectionStyle = UITableViewCellSelectionStyleNone;
        _cell.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
        _cell.titleLabel.text = [_nameArray objectAtIndex:indexPath.row];
        
        [_cell.image setImageURLStr:[NSString stringWithFormat:@"http://nyato.com%@",[_imgArray objectAtIndex:indexPath.row]] placeholder:[UIImage imageNamed:@"zhanwei"]];
        
        _cell.startLabel.text = [_startArray objectAtIndex:indexPath.row];
        
        _cell.endLabel.text = [_endArray objectAtIndex:indexPath.row];
        
        _cell.locationLabel.text = [_locationArray objectAtIndex:indexPath.row];
        
        _cell.typeLabel.text = [_sectionArray objectAtIndex:indexPath.row];
        [_cell.typeLabel sizeToFit];
        
        UIImage *image = [[UIImage alloc]init];
        if ([[_sectionArray objectAtIndex:indexPath.row] isEqualToString:@"同人展会"]) {
            image = [UIImage imageNamed:@"label_04"];
        } else if ([[_sectionArray objectAtIndex:indexPath.row] isEqualToString:@"动漫/游戏展"]) {
            image = [UIImage imageNamed:@"label2_09"];
        } else {
            image = [UIImage imageNamed:@"cos_09"];
        }
        
        _cell.typeImageV.frame = CGRectMake(7, 8, image.size.width, image.size.height);
        _cell.typeImageV.image = image;
        
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 78, 320, 1)];
        line.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.86 alpha:1];
        [_cell.contentView addSubview:line];
        
        return _cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 1) {
        [_place setTitle:[_placeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{
            _placeTable.frame = CGRectMake(184, 64, 66, 0);
        }completion:^(BOOL finished) {
            NSLog(@"动画执行完成后  要做的事情");
        }];
        _province = [_provinceArray objectAtIndex:indexPath.row];
        [self sendRequest:[_provinceArray objectAtIndex:indexPath.row] :_month.titleLabel.text];
    } else if(tableView.tag == 2) {
        [_month setTitle:[_monthArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{
            _monthTable.frame = CGRectMake(254, 64, 60, 0);
        }completion:^(BOOL finished) {
            NSLog(@"动画执行完成后  要做的事情");
        }];
        if ([_province isEqualToString:@""]) {
            [self sendRequest:_place.titleLabel.text :[_monthArray objectAtIndex:indexPath.row]];
        } else {
            [self sendRequest:_province :[_monthArray objectAtIndex:indexPath.row]];
        }
    } else if (tableView.tag == 3) {
        NewsInfoViewController *info = [[NewsInfoViewController alloc]init];
        info.eid = [_eidArray objectAtIndex:indexPath.row];
        info.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:info animated:YES];
        [MobClick event:@"goToNews"];

    }
}
-(void)sendRequest:(NSString *)place :(NSString *)month {
    if ([place isEqualToString:@"全部"] || [place isEqualToString:@"地点"] ) {
        place = @"";
    } else {
        
    }
    if ([month isEqualToString:@"月份"] || [month isEqualToString:@"全部"]) {
        month = @"";
    } else if (month.length == 2) {
        month = [month substringWithRange:NSMakeRange(0,1)];
    } else {
        month = [month substringWithRange:NSMakeRange(0,2)];
    }
    _url = [NSURL URLWithString:[NSString stringWithFormat:@"http://nyato.com/index.php?app=exhibition&mod=Adapi&act=ex_list&keyword=&province=%@&month=%@",place,month]];
    [self getFirstData];
}
- (void)scrollFullScreen:(NJKScrollFullScreen *)proxy scrollViewDidScrollUp:(CGFloat)deltaY
{
    [self moveNavigtionBar:deltaY animated:YES];
    [self moveToolbar:-deltaY animated:YES]; // move to revese direction
}

- (void)scrollFullScreen:(NJKScrollFullScreen *)proxy scrollViewDidScrollDown:(CGFloat)deltaY
{
    [self moveNavigtionBar:deltaY animated:YES];
    [self moveToolbar:-deltaY animated:YES];
}

- (void)scrollFullScreenScrollViewDidEndDraggingScrollUp:(NJKScrollFullScreen *)proxy
{
    [self hideNavigationBar:YES];
    [self hideToolbar:YES];
}

- (void)scrollFullScreenScrollViewDidEndDraggingScrollDown:(NJKScrollFullScreen *)proxy
{
    [self showNavigationBar:YES];
    [self showToolbar:YES];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [_scrollProxy reset];
    [self showNavigationBar:YES];
    [self showToolbar:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
